package com.milling.web.controller.business;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.milling.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.milling.common.annotation.Log;
import com.milling.common.core.controller.BaseController;
import com.milling.common.core.domain.AjaxResult;
import com.milling.common.enums.BusinessType;
import com.milling.business.domain.WmsInStock;
import com.milling.business.service.IWmsInStockService;
import com.milling.common.utils.poi.ExcelUtil;
import com.milling.common.core.page.TableDataInfo;

/**
 * 入库信息Controller
 *
 * @author X
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/business/instock")
public class WmsInStockController extends BaseController
{
    @Autowired
    private IWmsInStockService wmsInStockService;

    /**
     * 查询入库信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:instock:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsInStock wmsInStock)
    {
        startPage();
        List<WmsInStock> list = wmsInStockService.selectWmsInStockList(wmsInStock);
        return getDataTable(list);
    }

    /**
     * 导出入库信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:instock:export')")
    @Log(title = "入库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsInStock wmsInStock)
    {
        List<WmsInStock> list = wmsInStockService.selectWmsInStockList(wmsInStock);
        ExcelUtil<WmsInStock> util = new ExcelUtil<WmsInStock>(WmsInStock.class);
        util.exportExcel(response, list, "入库信息数据");
    }

    /**
     * 获取入库信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:instock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsInStockService.selectWmsInStockById(id));
    }

    /**
     * 新增入库信息
     */
    @PreAuthorize("@ss.hasPermi('business:instock:add')")
    @Log(title = "入库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsInStock wmsInStock)
    {
        String MacCode = "B001"; //当前机器码

        String m_Drill_Size = wmsInStock.getmDrillSize();    //钻针尺寸
        String m_Drill_Type = wmsInStock.getmDrillType();    //钻针类型
        String m_Drill_Count = wmsInStock.getmDrillCount().toString(); //钻针数量
        String m_Drill_Grind = wmsInStock.getmDrillGrindCount();    //钻针研次

        //默认刃长 和 厂牌都为1
        String m_Drill_Edge = "1";    //钻针刃长
        String m_Drill_Factory ="1";    //钻针厂牌
        String m_Drill_Kepp = "0"; //保留位
        String drillDirection = "1";    //钻针方向 倒插
        String boxType = "01";    //针盒类型 成盅盒

        String grindStatus = wmsInStock.getGrindStatus();    //研磨状态

        String m_Drill_Status = "0"; // 初始值根据需求设置
        String drillStatus = drillDirection  + grindStatus + boxType; //拼接字符串 钻针方向 + 研磨状态 + 针盒类型
        switch (drillStatus) {
            case "0000":
                m_Drill_Status = "0";
                break;
            case "0001":
                m_Drill_Status = "1";
                break;
            case "0010":
                m_Drill_Status = "2";
                break;
            case "0011":
                m_Drill_Status = "3";
                break;
            case "0100":
                m_Drill_Status = "4";
                break;
            case "0101":
                m_Drill_Status = "5";
                break;
            case "0110":
                m_Drill_Status = "6";
                break;
            case "0111":
                m_Drill_Status = "7";
                break;
            case "1000":
                m_Drill_Status = "8";
                break;
            case "1001":
                m_Drill_Status = "9";
                break;
            case "1010":
                m_Drill_Status = "A";
                break;
            case "1011":
                m_Drill_Status = "B";
                break;
            case "1100":
                m_Drill_Status = "C";
                break;
            case "1101":
                m_Drill_Status = "D";
                break;
            case "1110":
                m_Drill_Status = "E";
                break;
            case "1111":
                m_Drill_Status = "F";
                break;
            default:
                logger.error("未知的钻探状态，无效的参数");
                // 可添加默认情况的处理逻辑
                break;
        }
        String drillInfo = m_Drill_Size + m_Drill_Type + m_Drill_Edge + m_Drill_Factory + m_Drill_Grind + m_Drill_Status + m_Drill_Count + m_Drill_Kepp;
        wmsInStock.setMacCode(MacCode);
        wmsInStock.setmDrillStatus(m_Drill_Status);
        wmsInStock.setmDrillInfo(drillInfo);
        wmsInStock.setmDrillEdge(m_Drill_Edge);
        wmsInStock.setmDrillFactory(m_Drill_Factory);
        wmsInStock.setmDrillKepp(m_Drill_Kepp);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("MacCode", MacCode);
        jsonObject.put("TransCode", UUID.randomUUID());
        jsonObject.put("Drillinfo", "");
        jsonObject.put("m_Drill_Info", drillInfo);
        jsonObject.put("m_Drill_Size", m_Drill_Size);
        jsonObject.put("m_Drill_Type", m_Drill_Type);
        jsonObject.put("m_Drill_Edge", m_Drill_Edge);
        jsonObject.put("m_Drill_Factory", m_Drill_Factory);
        jsonObject.put("m_Drill_GrindCount", m_Drill_Grind);
        jsonObject.put("m_Drill_Status", m_Drill_Status);
        jsonObject.put("m_Drill_Count", m_Drill_Count);
        jsonObject.put("m_Drill_Kepp", m_Drill_Kepp);
        jsonObject.put("Timestamp", DateUtils.getNowDate().toString());
        logger.info("上送数据包" + jsonObject.toString());

        return toAjax(wmsInStockService.insertWmsInStock(wmsInStock, jsonObject.toString()));
    }

    /**
     * 修改入库信息
     */
    @PreAuthorize("@ss.hasPermi('business:instock:edit')")
    @Log(title = "入库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsInStock wmsInStock)
    {
        return toAjax(wmsInStockService.updateWmsInStock(wmsInStock));
    }

    /**
     * 删除入库信息
     */
    @PreAuthorize("@ss.hasPermi('business:instock:remove')")
    @Log(title = "入库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsInStockService.deleteWmsInStockByIds(ids));
    }
}
